from block_table_entry import BlockTableEntry
import time

class BlockTable():
	def __init__(self):
		self.bt = []
		btEntry = BlockTableEntry("mainBT_"+str(time.time()),None)
		self.bt.append(btEntry)
		self.tempStIdx = 0

	def getMainBlockTableEntry(self):
		if len(self.bt) == 0:
			return None
		else:
			return self.bt[0]


	def addBlockTableEntry(self,name,formerBtEntry):
		btEntry = BlockTableEntry(name,formerBtEntry)
		self.bt.append(btEntry)
		return btEntry

	def removeBlockTableEntry(self,name):
		idx = 0
		for btEntry in self.bt:
			if btEntry.getName() == name:
				self.bt.pop(idx)
			else:
				idx += 1

	def getBlockTableEntry(self,name):
		for entry in self.bt:
			if (entry.getName() == name):
				return entry
		return None
	def getSymbolTable(self,name):
		blockTableEntry = self.getBlockTableEntry(name)
		return blockTableEntry.getSymbolTable()

	def getTempStIdx(self):
		return self.tempStIdx
	
	def incrementTempStIdx(self):
		self.tempStIdx += 1

	def getNewTempStName(self):
		name = "stTemp%d"%(self.tempStIdx)
		self.incrementTempStIdx()
		return name

	def finishMainSymbolTable(self):
		btEntry = self.getMainBlockTableEntry()
		sTable = btEntry.getSymbolTable()
		sTable.finish()
		#print "#####################FINISH#######################"
		#sTable.printAll()
		#print "#####################"
		#finish main symbol table

	def printAll(self):
		print "##################"
		print "Tabla de Bloques: "
		print "##################"
		for btEntry in self.bt:
			btEntry.printAll()
